package com.lushu.chapitre1.graphics.elements.characters
{
	import com.lushu.chapitre1.graphics.animation.UserAnimatedBitmap;
	import com.lushu.chapitre1.graphics.elements.MovableElement;
	import com.lushu.chapitre1.graphics.elements.animals.Animal;
	import com.lushu.chapitre1.graphics.elements.enums.AnimatedObjectType;
	import com.lushu.graphics.engine.animation.AnimatedBitmap;
	import com.lushu.palettes.BodyPaletteData;
	import com.lushu.palettes.EyePaletteData;
	import com.lushu.palettes.HairPaletteData;
	import com.lushu.pools.AnimatedBitmapPool;
	
	import flash.events.Event;
	import flash.geom.Point;
	import com.lushu.chapitre1.data.vo.UserAvatarVO;

	public class UserCharacter extends MovableElement
	{
		private var _avatarInfo : UserAvatarVO;
		private var _temporaryBitmap : AnimatedBitmap;
		private var _headgearBitmap : AnimatedBitmap;
		private var _coupeBitmap : AnimatedBitmap;
		
		public function UserCharacter(animatedBitmapType:String, characterInfo : UserAvatarVO)
		{
			_avatarInfo = characterInfo;
			super(animatedBitmapType);
		}
		
		protected override function initElement() : void
		{
			_position = new Point();
			_temporaryBitmap = AnimatedBitmapPool.getFromPool(_animatedBitmapType);
			if(!_temporaryBitmap.isLoaded)
			{
				_temporaryBitmap.addEventListener("animationLoaded", onAnimationLoaded);
			}
			else
			{
				onAnimationLoaded();
			}
			
			
		}
		
		protected override function onAnimationLoaded(e : Event = null) : void
		{
			_animatedBitmap = new UserAnimatedBitmap(_temporaryBitmap);
				
			_currentAnimation = _animatedBitmap.getAnimationData(_currentFace, _currentAction);
			_currentAnimationLength = _currentAnimation.length;
			
			_coupeBitmap = AnimatedBitmapPool.getFromPool(AnimatedObjectType.COUPE_TEST);
			if(!_coupeBitmap.isLoaded)
			{
				_coupeBitmap.addEventListener("animationLoaded", onCoupeLoaded);
			}
			else
			{
				onCoupeLoaded();
			}
			
//			_headgearBitmap = AnimatedBitmapPool.getFromPool(AnimatedObjectType.HEADGEAR_TEST);
//			if(!_headgearBitmap.isLoaded)
//			{
//				_headgearBitmap.addEventListener("animationLoaded", onHeadgerLoaded);
//			}
//			else
//			{
//				onHeadgerLoaded();
//			}
		}
		
		protected function onHeadgerLoaded(e : Event = null) : void
		{
			(_animatedBitmap as UserAnimatedBitmap).add(_headgearBitmap);
		}
		
		protected function onCoupeLoaded(e : Event = null) : void
		{
			(_animatedBitmap as UserAnimatedBitmap).add(_coupeBitmap);
			(_animatedBitmap as UserAnimatedBitmap).setPalettes(BodyPaletteData.getPalette("bronze_palette"), HairPaletteData.getPalette("bleu01_palette"), EyePaletteData.getPalette("vert01_palette"));
			
		}
		
	}
}